#include "sfplotscanview.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QMouseEvent>

sfPlotScanView::sfPlotScanView(QWidget *parent) : QGraphicsView(parent)
{
  setFrameShadow(QFrame::Plain);
  setBackgroundBrush(Qt::gray);
  setScene(&MyScene);
  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

  MyItemPlotter  = MyScene.addRect(0,0,120,80, QPen(), QBrush(QColor(200,200,200)));
  MyItemPlotter->setZValue(0);
  MyItemMaterial = MyScene.addRect(0,0,60,60, QPen(), QBrush(QColor(150,150,255)));
  MyItemMaterial->setZValue(1);
  MyItemArea     = MyScene.addRect(0,0,40,40);
  MyItemArea->setZValue(2);
  MyItemData     = MyScene.addPath(MyData, QPen(Qt::yellow, 0));
  MyItemData->setZValue(3);
  setCacheMode(CacheBackground);
  setViewportUpdateMode(FullViewportUpdate);
  setRenderHint(QPainter::Antialiasing);
  setTransformationAnchor(AnchorUnderMouse);
  setResizeAnchor(AnchorUnderMouse);
  setMinimumSize(width(), height());
}
void sfPlotScanView::scaleView(qreal /*scaleFactor*/)
{
/*  qreal fx = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
  qreal fy = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).height();
  qreal factor = qMax(fx, fy);*/
}
void sfPlotScanView::wheelEvent(QWheelEvent */*event*/)
{
  //scaleView(pow((double)2, -(event->delta()) / 240.0));
}
/*
  Keyboard Events
*/
void sfPlotScanView::focusOutEvent(QFocusEvent * /*focusEvent*/ )
{
  k.terminate();
}
void sfPlotScanView::focusInEvent(QFocusEvent * /*focusEvent*/ )
{
 k.start();  
}
void sfPlotScanView::keyPressEvent( QKeyEvent * /*event*/ )
{
/*  s.state = PLOTTING;
  switch(event->key())
  {
  case Qt::Key_Left:
 //  s.line(-s.dstep * s.mx,0,0);
    s.line(-10000 * s.mx,0,0);
    break;
  case Qt::Key_Right:
    //s.line(s.dstep * s.mx,0,0);
    s.line(10000 * s.mx,0,0);
    break;
  case Qt::Key_Up:
    s.line(0,-s.dstep * s.my,0);
    break;
  case Qt::Key_Down:
    s.line(0,s.dstep * s.my,0);
    break;
  case Qt::Key_PageUp:
    s.line(0,0,-s.dstep * s.mz);
    break;
  case Qt::Key_PageDown:
    s.line(0,0,s.dstep * s.mz);
    break;
  }
  s.start();*/
}
void sfPlotScanView::keyReleaseEvent( QKeyEvent * /*event*/ )
{
  /*if(event->key() == Qt::Key_Shift)
    MyShiftPressed = false;*/
  //s.state = CANCELED;
}
QVector3D sfPlotScanView::setData(QList<QVector3D*> d, bool mirrorx, bool mirrory)
{
  QVector3D max;
  delete MyItemData;
  MyData= QPainterPath();
  if(d.size()== 0) return max;
  if(mirrory || mirrorx)
  {
    max = *d.first();
    foreach(QVector3D *v, d)
    {
      if(v->x() > max.x()) max.setX(v->x());
      if(v->y() > max.y()) max.setY(v->y());
      if(mirrory)
        v->setY(-v->y());
      if(mirrorx)
        v->setX(-v->x());
    }
    foreach(QVector3D *v, d)
    {
      if(mirrorx)
        v->setX(v->x()+max.x());
      if(mirrory)
        v->setY(v->y()+max.y());
    }
  }
  MyData.moveTo(d.at(0)->toPointF());
  for(int i = 0; i < d.size(); i++)
  {
    if(d.at(i)->z() == 0)
      MyData.moveTo(d.at(i)->toPointF());
    else
      MyData.lineTo(d.at(i)->toPointF());
  }
  MyItemData = MyScene.addPath(MyData, QPen(Qt::yellow, 0));
  MyItemData->setZValue(4);
  update();
  return max;
}

void sfPlotScanView::mousePressEvent ( QMouseEvent * event )
{
  QGraphicsView::mousePressEvent( event );
}
void sfPlotScanView::mouseMoveEvent ( QMouseEvent * event )
{
  QGraphicsView::mouseMoveEvent( event );
  //MyItemPlotter->setRect(0,0, event->pos().x(), event->pos().y());
}

void sfPlotScanView::mouseDoubleClickEvent ( QMouseEvent * event )
{
  QGraphicsView::mouseDoubleClickEvent(event);
 }

void sfPlotScanView::resizeEvent ( QResizeEvent * event )
{
  QGraphicsView::resizeEvent(event);
}

/**
  Mouse releases on the scene
*/
void sfPlotScanView::mouseReleaseEvent( QMouseEvent * event )
{
  QGraphicsView::mouseReleaseEvent(event);
}
